package com.zx.tradeclient.ctrl.index3;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.util.ActivityTaskManager;
import com.zx.tradeclient.R;

public class LeaveMessageActivity extends BaseActivity implements OnClickListener {
	/** Called when the activity is first created. */

	private EditText company_message_addressee;
	private EditText company_message_theme;
	private EditText company_message_content;
	private ProgressDialog progressDialog;

	private String mMyCompanyID = "";
	private String mSendCompanyID = "";
	private String mSendCompanyNmae = "";
	private String mMessageID = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.company_leave_message);

		if (!(ActivityTaskManager.getInstance().containsName("LeaveMessageActivity"))) {
			ActivityTaskManager.getInstance().putActivity("LeaveMessageActivity", this);
		}

		init();

	}

	private void init() {

		setHeader(0);

		header_right.setText("发送");
		header_right.setVisibility(View.VISIBLE);
		header_right.setOnClickListener(this);

		mMessageID = getIntent().getStringExtra("messageID");
		mMyCompanyID = getIntent().getStringExtra("mycompanyID");
		mSendCompanyID = getIntent().getStringExtra("sendcompanyID");
		mSendCompanyNmae = getIntent().getStringExtra("sendcompanyName");

		company_message_addressee = (EditText) findViewById(R.id.company_message_addressee);
		company_message_addressee.setText(mSendCompanyNmae);

		company_message_theme = (EditText) findViewById(R.id.company_message_theme);
		company_message_content = (EditText) findViewById(R.id.company_message_content);

	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == header_left) {
			goback();
		} else if (v == header_right) {
			progressDialog = ProgressDialog.show(LeaveMessageActivity.this, null, "正在获取数据，请稍后.....", true, true);
			sendLeaveMessageInfo();
		}

	}

	private void sendLeaveMessageInfo() {

		Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
		map.put("act", "leaveMessage");
		if(!(mMessageID.equals(""))){
			map.put("id", mMessageID);
		}
		map.put("sendId", mMyCompanyID);
		map.put("recvId", mSendCompanyID);
		map.put("title", company_message_theme.getText().toString());
		map.put("content", company_message_content.getText().toString());
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				// 解析数据

				try {
					String succeed = response.getString("succeed");

					if (succeed.equals("000")) {
						Toast toast2 = Toast.makeText(LeaveMessageActivity.this, "发送留言成功！", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();
						goback();

					} else {
						Toast toast2 = Toast.makeText(LeaveMessageActivity.this, "无数据", Toast.LENGTH_SHORT);
						toast2.setGravity(Gravity.CENTER, 0, 0);
						toast2.show();
					}

					progressDialog.dismiss();

				} catch (JSONException e) {
					progressDialog.dismiss();
					e.printStackTrace();
				}

			}

			@Override
			public void onFailure(Throwable e) {
				progressDialog.dismiss();
				Toast toast2 = Toast.makeText(LeaveMessageActivity.this, "错误", Toast.LENGTH_SHORT);
				toast2.setGravity(Gravity.CENTER, 0, 0);
				toast2.show();

			}
		});
	}

}